package com.sunny.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.sunny.dao.AttentionsMapper;
import com.sunny.dao.HouseMapper;
import com.sunny.dao.UserMapper;
import com.sunny.pojo.Attentions;
import com.sunny.pojo.House;
import com.sunny.service.IAttentionsService;
import com.sunny.utils.DateTimeUtil;
import com.sunny.utils.PageInformation;
import com.sunny.utils.PageUtil;
import com.sunny.utils.ServerResponse;
import com.sunny.vo.AttentionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName:
 * @Description:
 * @author: yubao.jin
 * @date: 2018/5/13
 * @version:V1.0
 */
@Service("iAttentionsService")
public class AttentionsServiceImpl implements IAttentionsService {
    @Autowired
    AttentionsMapper attentionsMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    HouseMapper houseMapper;

    @Override
    public ServerResponse add(Attentions attentions) {
        attentions.setSellerid(houseMapper.selectByPrimaryKey(attentions.getHouseid()).getSellerid());
        int resoultCount = attentionsMapper.insertSelective(attentions);
        if(resoultCount == 0) {
            return ServerResponse.createByErrorMessage("关注失败");
        }

        return ServerResponse.createBySuccessMessage("关注成功");
    }

    @Override
    public ServerResponse del(String ids, Integer buyerId) {
        // 字符串转化集合List
        List<String> idList = Splitter.on(",").splitToList(ids);
        int resultCount = attentionsMapper.deleteByIds(idList, buyerId);
        // 检验
        if(resultCount == 0) {
            return ServerResponse.createByErrorMessage("取消关注失败");
        }

        return ServerResponse.createBySuccessMessage("取消关注成功");
    }

    @Override
    public ServerResponse delAttentions(String ids, Integer buyerId) {
        int resultCount = attentionsMapper.deleteByHouseId(ids, buyerId);
        // 检验
        if(resultCount == 0) {
            return ServerResponse.createByErrorMessage("删除失败");
        }

        return ServerResponse.createBySuccessMessage("删除成功");
    }

    @Override
    public PageInformation<AttentionVo> list(PageUtil page, Attentions attentions) {
        // 利用pageHelper进行分页查询
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<Attentions> list = attentionsMapper.selectAttentionsByAttentios(attentions);
        PageInfo<Attentions> pageInfo = new PageInfo(list);
        List<AttentionVo> attentionVoList = Lists.newArrayList();
        // 组装
        for(Attentions a : pageInfo.getList()) {
            AttentionVo atten = new AttentionVo();
            atten.setPhone(userMapper.getUserPhoneById(a.getBuyerid()));
            House house = houseMapper.selectByPrimaryKey(a.getHouseid());
            atten.setHouseAddr(house.getCity() + " " + house.getArea() + " " + house.getStreet());
            atten.setHouseImage(house.getImage());
            atten.setHouseDes(house.getDescription());
            atten.setAttentionTime(DateTimeUtil.dateToStr(a.getCreateTime()));
            atten.setId(a.getId());
            atten.setAttentions(a);
            attentionVoList.add(atten);
        }

        // 组装PageInformation返回前端数据
        PageInformation<AttentionVo> pageInformation =  PageInformation.getInstance();
        pageInformation.setRows(attentionVoList);
        pageInformation.setTotal(pageInfo.getTotal());

        return pageInformation;
    }

    @Override
    public ServerResponse search(Attentions attentions) {
        Attentions attention = attentionsMapper.selectByPrimaryKey(attentions.getId());
        if(attention == null) {
            return ServerResponse.createByErrorMessage("查询失败");
        }

        return ServerResponse.createBySuccess("查询成功", attention);
    }
}
